home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #6 / Amiga Plus CD - 1996 - No. 06.iso / pd / programmierung / proasm / routines / crc16.r < prev    next >
Text File  |  1995-08-17  |  4KB  |  163 lines

  1.  
  2. ;---;  CRC16.r  ;--------------------------------------------------------------
  3. *
  4. *    ****    ANSI CRC16    ****
  5. *
  6. *    Author        Daniel Weber
  7. *    Version        1.20
  8. *    Last Revision    23.06.93
  9. *    Identifier    c16_defined
  10. *       Prefix        c16_    (CRC16)
  11. *                 ¯   ¯¯
  12. *    Functions    CRC16
  13. *            GenCRC16Table    (only available if CRC16.GenTable is
  14. *                     defined)
  15. *
  16. *    Table        CRC16table    used by the 'CalcCRC16' routine
  17. *                    (512 bytes).
  18. *
  19. *    Flags        CRC16.GenTable    enable 'GenCRC16Table', disable
  20. *                    'CRC16table'.
  21. *
  22. ;------------------------------------------------------------------------------
  23.  
  24. ;------------------
  25.     ifnd    c16_defined
  26. c16_defined    SET    1
  27.  
  28. ;------------------
  29. c16_oldbase    equ __BASE
  30.     base    c16_base
  31. c16_base:
  32.  
  33. ;------------------
  34.     opt    sto,o+,ow-        ;all optimisations on
  35.  
  36. ;------------------
  37.  
  38.  
  39. ;------------------------------------------------------------------------------
  40. *
  41. * CRC16        calculate a CRC16 checksum
  42. * CRC16D0    calculate a CRC16 checksum (partial)
  43. *
  44. * INPUT:    d0: checksum of the last part (only for CRC16D0!!)
  45. *        d7: #of bytes to be checksumed (size of area)
  46. *        a0: start of area
  47. *
  48. * RESULT:    d0: CRC16checksum
  49. *
  50. * NOTE:        CRC16D0 might be used to calculate a crc16 checksum in more
  51. *        than one step. D0 must be set to the checksum of the last
  52. *        part (or to zero for the first part).
  53. *
  54. ;------------------------------------------------------------------------------
  55.  
  56. CRC16:
  57.     moveq    #0,d0
  58. ;------------------
  59. CRC16D0:
  60.     movem.l    d1-a6,-(a7)
  61.     lea    CRC16table(pc),a1    ;CRC16 table for speed up
  62.     move.w    #$ff,d1
  63.     moveq    #0,d2
  64.     moveq    #1,d3
  65. \crcloop:
  66.     move.b    (a0)+,d2        ;take a byte
  67.     eor.w    d2,d0
  68.     move.w    d0,d5
  69.     and.w    d1,d0
  70.     add.w    d0,d0
  71.     move.w    (a1,d0.w),d0
  72.     lsr.w    #8,d5
  73.     eor.w    d5,d0
  74.     subq.l    #1,d7
  75.     bne.s    \crcloop
  76.     movem.l    (a7)+,d1-a6
  77.     rts
  78.  
  79.  
  80.  
  81.  
  82. ;------------------------------------------------------------------------------
  83. *
  84. * GenCRC16Table    - generate a 512 byte table for the CRC16 checksumer
  85. *
  86. ;------------------------------------------------------------------------------
  87.     IFD    CRC16.GenTale
  88. GenCRC16Table:
  89. ;------------------
  90.     lea    CRC16table(pc),a0        ;space for the table
  91.     move    #0,d7
  92.  
  93. \loop:    move.w    d7,d6
  94.     moveq    #7,d5
  95.  
  96. \loop2:    lsr.w    #1,d6
  97.     bcc.s    1$
  98.     eor.w    #$a001,d6
  99. 1$:    dbra    d5,\loop2
  100.     move.w    d6,(a0)+
  101.     addq.w    #1,d7
  102.     cmp.w    #256,d7
  103.     blt.s    \loop
  104.     rts
  105.     ENDC
  106.  
  107.  
  108. ;------------------------------------------------------------------------------
  109. *
  110. * Table to calculate the CRC16 checksum (512 bytes)
  111. *
  112. ;------------------------------------------------------------------------------
  113.  
  114.     IFND    CRC16.GenTable
  115. CRC16table:
  116.     dc.w  $0000,$c0c1,$c181,$0140,$c301,$03c0,$0280,$c241
  117.     dc.w  $c601,$06c0,$0780,$c741,$0500,$c5c1,$c481,$0440
  118.     dc.w  $cc01,$0cc0,$0d80,$cd41,$0f00,$cfc1,$ce81,$0e40
  119.     dc.w  $0a00,$cac1,$cb81,$0b40,$c901,$09c0,$0880,$c841
  120.     dc.w  $d801,$18c0,$1980,$d941,$1b00,$dbc1,$da81,$1a40
  121.     dc.w  $1e00,$dec1,$df81,$1f40,$dd01,$1dc0,$1c80,$dc41
  122.     dc.w  $1400,$d4c1,$d581,$1540,$d701,$17c0,$1680,$d641
  123.     dc.w  $d201,$12c0,$1380,$d341,$1100,$d1c1,$d081,$1040
  124.     dc.w  $f001,$30c0,$3180,$f141,$3300,$f3c1,$f281,$3240
  125.     dc.w  $3600,$f6c1,$f781,$3740,$f501,$35c0,$3480,$f441
  126.     dc.w  $3c00,$fcc1,$fd81,$3d40,$ff01,$3fc0,$3e80,$fe41
  127.     dc.w  $fa01,$3ac0,$3b80,$fb41,$3900,$f9c1,$f881,$3840
  128.     dc.w  $2800,$e8c1,$e981,$2940,$eb01,$2bc0,$2a80,$ea41
  129.     dc.w  $ee01,$2ec0,$2f80,$ef41,$2d00,$edc1,$ec81,$2c40
  130.     dc.w  $e401,$24c0,$2580,$e541,$2700,$e7c1,$e681,$2640
  131.     dc.w  $2200,$e2c1,$e381,$2340,$e101,$21c0,$2080,$e041
  132.     dc.w  $a001,$60c0,$6180,$a141,$6300,$a3c1,$a281,$6240
  133.     dc.w  $6600,$a6c1,$a781,$6740,$a501,$65c0,$6480,$a441
  134.     dc.w  $6c00,$acc1,$ad81,$6d40,$af01,$6fc0,$6e80,$ae41
  135.     dc.w  $aa01,$6ac0,$6b80,$ab41,$6900,$a9c1,$a881,$6840
  136.     dc.w  $7800,$b8c1,$b981,$7940,$bb01,$7bc0,$7a80,$ba41
  137.     dc.w  $be01,$7ec0,$7f80,$bf41,$7d00,$bdc1,$bc81,$7c40
  138.     dc.w  $b401,$74c0,$7580,$b541,$7700,$b7c1,$b681,$7640
  139.     dc.w  $7200,$b2c1,$b381,$7340,$b101,$71c0,$7080,$b041
  140.     dc.w  $5000,$90c1,$9181,$5140,$9301,$53c0,$5280,$9241
  141.     dc.w  $9601,$56c0,$5780,$9741,$5500,$95c1,$9481,$5440
  142.     dc.w  $9c01,$5cc0,$5d80,$9d41,$5f00,$9fc1,$9e81,$5e40
  143.     dc.w  $5a00,$9ac1,$9b81,$5b40,$9901,$59c0,$5880,$9841
  144.     dc.w  $8801,$48c0,$4980,$8941,$4b00,$8bc1,$8a81,$4a40
  145.     dc.w  $4e00,$8ec1,$8f81,$4f40,$8d01,$4dc0,$4c80,$8c41
  146.     dc.w  $4400,$84c1,$8581,$4540,$8701,$47c0,$4680,$8641
  147.     dc.w  $8201,$42c0,$4380,$8341,$4100,$81c1,$8081,$4040
  148.     ENDC
  149.  
  150. ;--------------------------------------------------------------------
  151.  
  152. ;------------------
  153.     base    c16_oldbase
  154.  
  155. ;------------------
  156.     opt    rcl
  157.  
  158. ;------------------
  159.     endif
  160.  
  161.  end
  162.  
  163.